package cn.yr.blog.controller.management;

import cn.hutool.core.util.ObjectUtil;
import cn.yr.blog.service.VersionService;
import cn.yr.common.core.domain.entity.YrVersion;
import cn.yr.common.core.domain.vo.PageVo;
import cn.yr.common.exception.ret.Ret;
import cn.yr.common.security.annotation.PreAuthorize;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Date;


/**
 * 版本
 * @author Lang  1102076808@qq.com
 * @date 2020-12-27 18:01:14
 */
@Api(tags = "management版本")
@RestController
@RequestMapping("/management/version")
public class ManagerVersionController {
    @Autowired
    private VersionService versionService;

	/**
     * 分页列表
     */
	@RequestMapping("/page")
	public Ret page(@RequestBody PageVo<YrVersion> pageVo){
		QueryWrapper<YrVersion> wrapper = new QueryWrapper<>();
		wrapper.like(ObjectUtil.isNotEmpty(pageVo.getParams().get("tag")),"tag",pageVo.getParams().get("tag"));
		wrapper.like(ObjectUtil.isNotEmpty(pageVo.getParams().get("description")),"description",pageVo.getParams().get("description"));
		wrapper.orderByDesc("tag");
		PageVo<YrVersion> pageYrVersion = versionService.page(pageVo,wrapper);
		return Ret.success(pageYrVersion);
	}

    /**
     * 列表
     */
    @RequestMapping("/list")
    @PreAuthorize(hasPermi = "manager:version:list")
    public Ret list(){
        return Ret.success(versionService.list());
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @PreAuthorize(hasPermi = "manager:version:info")
    public Ret info(@PathVariable("id") Integer id){
		YrVersion  yrVersion = versionService.getById(id);
	    return Ret.success(yrVersion);
    }

    /**
     * 保存
     */
    @RequestMapping("/add")
    @PreAuthorize(hasPermi = "manager:version:add")
    public Ret save(@Validated @RequestBody YrVersion yrVersion){
	    yrVersion.setCreateTime(new Date());
		versionService.save(yrVersion);
	    return Ret.success();
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    @PreAuthorize(hasPermi = "manager:version:edit")
    public Ret update(@Validated @RequestBody YrVersion yrVersion){
		versionService.updateById(yrVersion);
	    return Ret.success();
    }

    /**
     * 删除
     */
    @RequestMapping("/remove/{ids}")
    @PreAuthorize(hasPermi = "manager:version:remove")
    public Ret delete(@PathVariable Integer[] ids){
		versionService.removeByIds(Arrays.asList(ids));
        return Ret.success();
    }

}
